package com.maxlen.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.concurrent.TimeUnit;

/**
 * redis 操作工具类
 */
public class RedisUtils {

    public static final String TOKEN_USER = "TOKEN_";

    public static void set(RedisTemplate redisTemplate, String key, Object data){
        String[] excludeProperties = {"authorities"};
        PropertyPreFilters filters = new PropertyPreFilters();
        PropertyPreFilters.MySimplePropertyPreFilter propertyPreFilter = filters.addFilter();
        propertyPreFilter.addExcludes(excludeProperties);
        redisTemplate.opsForValue().set(key, JSON.toJSONString(data, propertyPreFilter), 60, TimeUnit.MINUTES);
    }

    public static <T> T get(RedisTemplate redisTemplate, String key, Class<T> clazz){
        String value = (String) redisTemplate.opsForValue().get(key);
        return JSON.parseObject(value,clazz);
    }

    public static void delete(RedisTemplate redisTemplate, String key){
        redisTemplate.delete(key);
    }

    public static boolean isExist(RedisTemplate redisTemplate, String key){
        return redisTemplate.hasKey(key);
    }

}
